// pages/article/comment/commnet.js
import { 
  COMMENT_LIST,
 } from "../../request/api";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    listviewData:{
      loadStatus: 0,
      height: 0,
    },
    id: 0,
    type: 0,
    commentList: [],
    pageToken: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.data.id = options.id;
    that.data.type = options.type;
    that.loadMore();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  toUserProfile: function(e){
    var id = e.currentTarget.dataset.id;
    if(id){
      wx.navigateTo({
        url: '/pages/user/user?userId='+id,
      });
    }
  },

  refresh: function(){
    this.data.pageToken = '';
    this.data.commentList = [];
    this.loadMore();
  },

  loadMore: function(){
    this.getComments(this.data.id, this.data.type, this.data.pageToken);
  },
  getComments: function(id, type, pageToken){
    COMMENT_LIST({'sourceId': id, 'order': 1, 'parts': 'refer', 'type': type, 'pageToken': pageToken, }).then(res => {
      if(res.data.code == 1){
        var result = res.data.result;
        var list = result.items;
        if(pageToken.length > 0){
          list = this.data.commentList.concat(list);
        }
        this.setData({
          commentList: list,
          pageToken: res.data.result.nextPageToken,
          listviewData: {
            loadStatus: result.responseCount < result.requestCount? 3 : 2,
          }
        });
      } else {
      this.setLoadStatus(2);
      }
    }).catch(err => {
      this.setLoadStatus(2);
    });
  },

  setLoadStatus: function(status){
    this.setData({
      listviewData: {
        loadStatus: status,
      }
    });
  }
})